﻿namespace AddressBook.Business.Event
{
	#region "Using Statements"
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;

	using Csla;
	using Csla.Data;
	#endregion

	[Serializable]
	public class EventGuestList : BusinessListBase<EventGuestList, Guest>
	{
		#region "Business Methods"

		public Guest GetItem(Int32 guestId, GuestType guestType)
		{
			foreach (Guest guest in this)
			{
				if (guest.GuestId == guestId && guest.GuestType == guestType)
				{
					return guest;
				}
			}

			return null;
		}

		public void Add(Int32 guestId, GuestType guestType)
		{
			if (!(Contains(guestId, guestType)))
			{
				Guest resource = Guest.NewGuest(guestId, guestType);
				this.Add(resource);
			}
			else
			{
				throw new InvalidOperationException("This guest is already invited to that event");
			}
		}

		public void Remove(Int32 guestId, GuestType guestType)
		{
			foreach (Guest guest in this)
			{
				if (guest.GuestId == guestId && guest.GuestType == guestType)
				{
					Remove(guest);
					break;
				}
			}
		}

		public bool Contains(Int32 guestId, GuestType guestType)
		{
			foreach (Guest guest in this)
			{
				if (guest.GuestId == guestId && guest.GuestType == guestType)
				{
					return true;
				}
			}
			return false;
		}

		public bool ContainsDeleted(Int32 guestId, GuestType guestType)
		{
			foreach (Guest guest in DeletedList)
			{
				if (guest.GuestId == guestId && guest.GuestType == guestType)
				{
					return true;
				}
			}
			return false;
		}

		#endregion

		#region "Factory Methods"

		internal static EventGuestList NewEventGuestList()
		{
			return DataPortal.CreateChild<EventGuestList>();
		}

		internal static EventGuestList GetEventGuestList(AddressBook.Business.Model.Guest[] data)
		{
			return DataPortal.FetchChild<EventGuestList>(data);
		}

		private EventGuestList()
		{ /* require use of factory methods */ }

		#endregion

		#region "Data Access"

		private void Child_Fetch(AddressBook.Business.Model.Guest[] data)
		{
			if (data == null) return;

			this.RaiseListChangedEvents = false;

			foreach (var value in data)
			{
				this.Add(Guest.GetGuest(value));
			}

			this.RaiseListChangedEvents = true;
		}

		#endregion
	}
}
